package com.mcxx.modules.base.utils;


import com.mcxx.modules.base.entity.AdmindivisionEntity;
import com.mcxx.result.BaseResult;
import com.mcxx.util.ReflectUtil;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import org.apache.commons.lang3.StringUtils;

public class AdressUtil {

    public static String getAreaCode(BaseResult<AdmindivisionEntity> admindivisionEntity, String adressCode) {
        String areaCode = "";
        if (admindivisionEntity.getData() != null) {
            if (admindivisionEntity.getData().getLevel() == 1) {
                areaCode = adressCode.substring(0, 1);
            } else if (admindivisionEntity.getData().getLevel() == 2) {
                areaCode = adressCode.substring(0, 2);
            } else if (admindivisionEntity.getData().getLevel() == 3) {
                areaCode = adressCode.substring(0, 4);
            } else if (admindivisionEntity.getData().getLevel() == 4) {
                areaCode = adressCode.substring(0, 6);
            } else if (admindivisionEntity.getData().getLevel() == 5) {
                areaCode = adressCode.substring(0, 9);
            } else if (admindivisionEntity.getData().getLevel() == 6) {
                areaCode = adressCode;
            }
        }
        return areaCode;
    }

    public static String getLevelCode(Integer level, String areaCode) {
        String levelCode = "";
        if (level == 1) {
            levelCode = areaCode.substring(0, 2);
        } else if (level == 2) {
            levelCode = areaCode.substring(0, 4);
        } else if (level == 3) {
            levelCode = areaCode.substring(0, 6);
        } else if (level == 4) {
            levelCode = areaCode.substring(0, 9);
        }
        return levelCode;
    }


    public static String getAreaCode(String code){
        code = StringUtils.isNotEmpty(code) ? code : "43";
        code = "000".equals(code.substring(9,12))?code.substring(0,9):code;
        code = "000".equals(code.substring(6,9))?code.substring(0,6):code;
        code = "00".equals(code.substring(4,6))?code.substring(0,4):code;
        return  "00".equals(code.substring(2,4))?code.substring(0,2):code;
    }


    public static String getAreaCode(String code, UserAccountVo accountVo){
        if(StringUtils.isNotEmpty(code)){
             code =  getAreaCode(code);
        }else{
            if(accountVo != null  && StringUtils.isNotEmpty(accountVo.getAreaCode())){
                code = getAreaCode(accountVo.getAreaCode());
            }
        }
        return code ;
    }



    public static void reflectAreaCode(Object sourceObject,String sourceField,Object targetObject,String targetField){


        String sourceValue = ReflectUtil.getFieldValueByFieldName(sourceField,sourceObject);

        String targetValue = ReflectUtil.getFieldValueByFieldName(targetField,sourceObject);

        if(StringUtils.isNotEmpty(targetValue)){
            sourceValue = getAreaCode(targetValue);
        }else{
            sourceValue = getAreaCode(sourceValue);
        }

        ReflectUtil.setFieldValueByFieldName(targetField,targetObject,sourceValue);
    }


}
